package com.md.value.server;


import com.md.value.enums.DataSourceTypeEnum;
import lombok.experimental.UtilityClass;

/**
 * @author 刘骄阳
 * 2022-04-06 17:09
 */
@UtilityClass
public class DataSourceContextHolder {

  private static final ThreadLocal<DataSourceTypeEnum> CONTEXT_HOLDER =
    new InheritableThreadLocal<DataSourceTypeEnum>() {
    };

  public static Object get() {
    return CONTEXT_HOLDER.get();
  }

  public static void put(final DataSourceTypeEnum sourceType) {
    CONTEXT_HOLDER.set(sourceType);
  }

  public static void write() {
    CONTEXT_HOLDER.set(DataSourceTypeEnum.WRITE);
  }

  public static void read() {
    CONTEXT_HOLDER.set(DataSourceTypeEnum.READ);
  }

  public static boolean isWrite() {
    return CONTEXT_HOLDER.get() == DataSourceTypeEnum.WRITE;
  }

  public static boolean isRead() {
    return CONTEXT_HOLDER.get() == DataSourceTypeEnum.READ;
  }


  public static void clear() {
    CONTEXT_HOLDER.remove();
  }
}

